package cn.ibdsr.human.util;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * 用于 Spring webmvc 请求与响应对象从线程本地存取的工具类
 * @author chenglx
 * @version V1.0
 * @date 2020/7/7 17:42
 * <p>
 * Date Author Description
 * ------------------------------------------------------
 * 2020/7/7 chenglx init
 */
public class HttpServletUtils {

    /**
     * 当前线程处理的HTTP请求对象
     */
    private static final ThreadLocal<HttpServletRequest> THREAD_REQUEST = new ThreadLocal<HttpServletRequest>();
    /**
     * 设置当前线程处理的HTTP请求对象
     * @param request
     */
    public static void setRequest(HttpServletRequest request) {
        THREAD_REQUEST.set(request);
    }
    /**
     * 获取当前线程处理的HTTP请求对象
     * @return
     */
    public static HttpServletRequest getRequest() {
        return THREAD_REQUEST.get();
    }
    /**
     * 清除当前线程处理的HTTP请求对象
     */
    public static void removeRequest() {
        THREAD_REQUEST.remove();
    }


    /**
     * 当前线程处理的HTTP响应对象
     */
    private static final ThreadLocal<HttpServletResponse> THREAD_RESPONSE = new ThreadLocal<HttpServletResponse>();
    /**
     * 设置当前线程处理的HTTP响应对象
     * @param response
     */
    public static void setResponse(HttpServletResponse response) {
        THREAD_RESPONSE.set(response);
    }
    /**
     * 获取当前线程处理的HTTP响应对象
     * @return
     */
    public static HttpServletResponse getResponse() {
        return THREAD_RESPONSE.get();
    }
    /**
     * 清除当前线程处理的HTTP响应对象
     */
    public static void removeResponse(){
        THREAD_RESPONSE.remove();
    }

}
